package com.lrw.service;

import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.lrw.entity.UserModel;
import com.vingsoft.iframework.orm.hibernate.HibernateDao;

/**
 * 用户登录验证操作类
 * 
 * @since 2013-06-06
 * @author lrw
 * 
 */
@Service
public class UserService extends HibernateDao<UserModel, Integer> {
	private JdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
	}

	/**
	 * 用户信息验证
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public List<UserModel> getLogin(String username, String password) {
		String sql = "select * from tbl_user where username='" + username
				+ "' and password='" + password + "'";
		RowMapper<UserModel> rows = BeanPropertyRowMapper
				.newInstance(UserModel.class);
		return jdbcTemplate.query(sql, rows);
	}

	/**
	 * 查找用户列表
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public List<UserModel> findAll(String username, String password, String sex) {
		String sql = "select  *from tbl_user ";
		if (StringUtils.isNotEmpty(username)) {
			sql += " where username='" + username + "'";
		}
		if (StringUtils.isNotEmpty(password)) {
			sql += " where password='" + password + "'";
		}
		if (StringUtils.isNotEmpty(sex)) {
			sql += " where sex='" + sex + "'";
		}
		RowMapper<UserModel> rows = BeanPropertyRowMapper
				.newInstance(UserModel.class);
		return jdbcTemplate.query(sql, rows);
	}

	/**
	 * 用户新增或更新
	 * 
	 * @param userModel
	 */
	public void saveOrUpdate(UserModel userModel) {
		if (null != userModel) {
			if (userModel.getId() == 0) {
				jdbcTemplate
						.update(
								"insert into tbl_user(username,password,email,mobelphone,sex,age) values (?,?,?,?,?,?)",
								userModel.getUsername(), userModel
										.getPassword(), userModel.getEmail(),
								userModel.getModelphone(), userModel.getSex(),
								userModel.getAge());
			} else {
				jdbcTemplate.update("UPDATE tbl_user set username='"
						+ userModel.getUsername() + "' ,password='"
						+ userModel.getPassword() + "',email='"
						+ userModel.getEmail() + "',mobelphone='"
						+ userModel.getModelphone() + "',sex='"
						+ userModel.getSex() + "',age="+userModel.getAge()+"");
			}
		}

	}

	public void deleteById(Long id) {
		jdbcTemplate.update("delete from tbl_user where id='" + id + "'");

	}

	public List<UserModel> getAll(Integer id) {
		RowMapper<UserModel> rowMapper = BeanPropertyRowMapper
				.newInstance(UserModel.class);
		String sql = "select * from tbl_user where id=" + id + " ";
		return jdbcTemplate.query(sql, rowMapper);
	}
}
